Connecting to Cassandra Server via programming Languages
In order to store or access the data inside a Cassandra database, you first need to connect to the database server. We will show you the sample codes to connect your Cassandra database via JAVA and Python.
Connecting to Cassandra via JAVA
Before you start, you need to add Cassandra driver package to your Java project. If you use Maven, you can add the driver like this:
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>3.11.0</version>
</dependency>
Connect to Cassandra via Java with SSL
- Get the truststore at the connection information on the Overview page.
- Connect to Cassandra by issuing the codes below.
package cassandra_connection;
import java.io.InputStream;
import java.security.KeyStore;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.PlainTextAuthProvider;
import com.datastax.driver.core.RemoteEndpointAwareJdkSSLOptions;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Session;
public class CassandraConnection {
public static void main(String[] args) {
String host = "cassandra-xxxx-0.cloudclusters.net"; // change it to your database server name
int port = 5067; // change it to your database server port
String userName = "your database user name";
String password = "your database password";
String trustStoreLocation = "/cassandra.truststore.jks";
String trustStorePassword = "your trust store password";
SSLContext sslContext = null;
try {
InputStream is = CassandraConnection.class.getResourceAsStream(trustStoreLocation);
KeyStore keystore = KeyStore.getInstance("JKS");
char[] pwd = trustStorePassword.toCharArray();
keystore.load(is, pwd);
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(keystore);
TrustManager[] tm = tmf.getTrustManagers();
sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tm, null);
} catch (Exception e) {
System.out.println(e);
return;
}
RemoteEndpointAwareJdkSSLOptions sslOptions = RemoteEndpointAwareJdkSSLOptions.builder()
.withSSLContext(sslContext).build();
Cluster cluster = Cluster.builder().addContactPoints(host).withPort(port).withSSL(sslOptions)
.withAuthProvider(new PlainTextAuthProvider(userName, password)).build();
try {
cluster.init();
Session session = cluster.connect();
try {
ResultSet res = session.execute("SELECT * FROM system_auth.roles");
res.forEach(System.out::println);
} finally {
session.close();
}
} finally {
cluster.close();
}
}
}
Connect to Cassandra via Java without SSL
package cassandra_connection;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.PlainTextAuthProvider;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Session;
public class CassandraConnectionWithoutSSL {
public static void main(String[] args) {
String host = "cassandra-xxxx-0.cloudclusters.net"; // change it to your database server name
int port = 5067; // change it to your database server port
String userName = "your database user name";
String password = "your database password";
Cluster cluster = Cluster.builder().addContactPoints(host).withPort(port)
.withAuthProvider(new PlainTextAuthProvider(userName, password)).build();
try {
cluster.init();
Session session = cluster.connect();
try {
ResultSet res = session.execute("SELECT * FROM system_auth.roles");
res.forEach(System.out::println);
} finally {
session.close();
}
} finally {
cluster.close();
}
}
}
Connecting to Cassandra via Python
Install cassandra-driver
pip install cassandra-driver
Connect to Cassandra via Python with SSL
- Get the user cert at the connection information on the Overview page.
- Connect to Cassandra with the codes below:
from cassandra.cluster import Cluster
from ssl import SSLContext, PROTOCOL_TLSv1_2, CERT_REQUIRED
from cassandra.auth import PlainTextAuthProvider
class CassandraConnect(object):
host = 'your host'
port = 'your port'
username = 'your username'
password = 'your password'
def ssl_connect(self):
ssl_context = SSLContext(PROTOCOL_TLSv1_2)
ssl_context.load_verify_locations('your cert file')
ssl_context.verify_mode = CERT_REQUIRED
auth_provider = PlainTextAuthProvider(username=self.username, password=self.password)
cluster = Cluster([self.host], ssl_context=ssl_context, auth_provider=auth_provider, port=self.port)
session = cluster.connect()
r = session.execute('select * from system_schema.keyspaces')
print(r.current_rows)
if _name_ == '__main__':
Ca = CassandraConnect()
Ca.ssl_connect()
Connect to Cassandra via Python without SSL
from cassandra.cluster import Cluster
from ssl import SSLContext, PROTOCOL_TLSv1_2, CERT_REQUIRED
from cassandra.auth import PlainTextAuthProvider
class CassandraConnect(object):
host = 'your host'
port = 'your port'
username = 'your username'
password = 'your password'
def connect(self):
auth_provider = PlainTextAuthProvider(username=self.username, password=self.password)
cluster = Cluster([self.host], auth_provider=auth_provider, port=self.port)
session = cluster.connect()
r = session.execute('select * from system_schema.keyspaces')
print(r.current_rows)
if _name_ == '__main__':
Ca = CassandraConnect()
Ca.connect()
If any further assistance is needed, feel free to contact us.